<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 3 章 数据导入 | 极客R：数据分析之道</title>
  <meta name="description" content="一本极简 R 入门图书" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="第 3 章 数据导入 | 极客R：数据分析之道" />
  <meta property="og:type" content="book" />
  
  <meta property="og:image" content="cover.png" />
  <meta property="og:description" content="一本极简 R 入门图书" />
  <meta name="github-repo" content="ShixiangWang/geek-r-tutorial" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 3 章 数据导入 | 极客R：数据分析之道" />
  
  <meta name="twitter:description" content="一本极简 R 入门图书" />
  <meta name="twitter:image" content="cover.png" />

<meta name="author" content="王诗翔, 生信技能树" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="base.html"/>
<link rel="next" href="clean.html"/>
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/gitbook/css/style.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">数据分析之道</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>前言</a><ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#内容简介"><i class="fa fa-check"></i>内容简介</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#许可"><i class="fa fa-check"></i>许可</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#建议与反馈"><i class="fa fa-check"></i>建议与反馈</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#致谢"><i class="fa fa-check"></i>致谢</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>作者简介</a></li>
<li class="chapter" data-level="1" data-path="prepare.html"><a href="prepare.html"><i class="fa fa-check"></i><b>1</b> 准备工作</a><ul>
<li class="chapter" data-level="1.1" data-path="prepare.html"><a href="prepare.html#r-的下载和安装"><i class="fa fa-check"></i><b>1.1</b> R 的下载和安装</a></li>
<li class="chapter" data-level="1.2" data-path="prepare.html"><a href="prepare.html#rstudio-的下载和安装"><i class="fa fa-check"></i><b>1.2</b> RStudio 的下载和安装</a></li>
<li class="chapter" data-level="1.3" data-path="prepare.html"><a href="prepare.html#配置可选"><i class="fa fa-check"></i><b>1.3</b> 配置（可选）</a></li>
<li class="chapter" data-level="1.4" data-path="prepare.html"><a href="prepare.html#常见问题与方案"><i class="fa fa-check"></i><b>1.4</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="1.4.1" data-path="prepare.html"><a href="prepare.html#r-在-linux-系统下的安装"><i class="fa fa-check"></i><b>1.4.1</b> R 在 Linux 系统下的安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="prepare.html"><a href="prepare.html#rtools-安装"><i class="fa fa-check"></i><b>1.4.2</b> Rtools 安装</a></li>
<li class="chapter" data-level="1.4.3" data-path="prepare.html"><a href="prepare.html#rstudio-server-安装"><i class="fa fa-check"></i><b>1.4.3</b> RStudio Server 安装</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="base.html"><a href="base.html"><i class="fa fa-check"></i><b>2</b> 基础语法</a><ul>
<li class="chapter" data-level="2.1" data-path="base.html"><a href="base.html#基本数据结构"><i class="fa fa-check"></i><b>2.1</b> 基本数据结构</a><ul>
<li class="chapter" data-level="2.1.1" data-path="base.html"><a href="base.html#向量"><i class="fa fa-check"></i><b>2.1.1</b> 向量</a></li>
<li class="chapter" data-level="2.1.2" data-path="base.html"><a href="base.html#数组与矩阵"><i class="fa fa-check"></i><b>2.1.2</b> 数组与矩阵</a></li>
<li class="chapter" data-level="2.1.3" data-path="base.html"><a href="base.html#数据框"><i class="fa fa-check"></i><b>2.1.3</b> 数据框</a></li>
<li class="chapter" data-level="2.1.4" data-path="base.html"><a href="base.html#列表"><i class="fa fa-check"></i><b>2.1.4</b> 列表</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="base.html"><a href="base.html#控制结构"><i class="fa fa-check"></i><b>2.2</b> 控制结构</a><ul>
<li class="chapter" data-level="2.2.1" data-path="base.html"><a href="base.html#条件控制"><i class="fa fa-check"></i><b>2.2.1</b> 条件控制</a></li>
<li class="chapter" data-level="2.2.2" data-path="base.html"><a href="base.html#循环控制"><i class="fa fa-check"></i><b>2.2.2</b> 循环控制</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="base.html"><a href="base.html#函数与函数式编程"><i class="fa fa-check"></i><b>2.3</b> 函数与函数式编程</a><ul>
<li class="chapter" data-level="2.3.1" data-path="base.html"><a href="base.html#创建和使用函数"><i class="fa fa-check"></i><b>2.3.1</b> 创建和使用函数</a></li>
<li class="chapter" data-level="2.3.2" data-path="base.html"><a href="base.html#作用域"><i class="fa fa-check"></i><b>2.3.2</b> 作用域</a></li>
<li class="chapter" data-level="2.3.3" data-path="base.html"><a href="base.html#任意参数"><i class="fa fa-check"></i><b>2.3.3</b> 任意参数</a></li>
<li class="chapter" data-level="2.3.4" data-path="base.html"><a href="base.html#函数式编程"><i class="fa fa-check"></i><b>2.3.4</b> 函数式编程</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="base.html"><a href="base.html#三方包的安装与加载"><i class="fa fa-check"></i><b>2.4</b> 三方包的安装与加载</a><ul>
<li class="chapter" data-level="2.4.1" data-path="base.html"><a href="base.html#cran"><i class="fa fa-check"></i><b>2.4.1</b> CRAN</a></li>
<li class="chapter" data-level="2.4.2" data-path="base.html"><a href="base.html#bioconductor"><i class="fa fa-check"></i><b>2.4.2</b> Bioconductor</a></li>
<li class="chapter" data-level="2.4.3" data-path="base.html"><a href="base.html#github-等-git-库"><i class="fa fa-check"></i><b>2.4.3</b> GitHub 等 Git 库</a></li>
<li class="chapter" data-level="2.4.4" data-path="base.html"><a href="base.html#包使用"><i class="fa fa-check"></i><b>2.4.4</b> 包使用</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="base.html"><a href="base.html#编程实战roc-曲线计算与绘制"><i class="fa fa-check"></i><b>2.5</b> 编程实战：ROC 曲线计算与绘制</a><ul>
<li class="chapter" data-level="2.5.1" data-path="base.html"><a href="base.html#背景与目标"><i class="fa fa-check"></i><b>2.5.1</b> 背景与目标</a></li>
<li class="chapter" data-level="2.5.2" data-path="base.html"><a href="base.html#代码实现"><i class="fa fa-check"></i><b>2.5.2</b> 代码实现</a></li>
<li class="chapter" data-level="2.5.3" data-path="base.html"><a href="base.html#代码讲解"><i class="fa fa-check"></i><b>2.5.3</b> 代码讲解</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="base.html"><a href="base.html#常见问题与方案-1"><i class="fa fa-check"></i><b>2.6</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="2.6.1" data-path="base.html"><a href="base.html#复数表示"><i class="fa fa-check"></i><b>2.6.1</b> 复数表示</a></li>
<li class="chapter" data-level="2.6.2" data-path="base.html"><a href="base.html#与---的区别"><i class="fa fa-check"></i><b>2.6.2</b> = 与 &lt;- 的区别</a></li>
<li class="chapter" data-level="2.6.3" data-path="base.html"><a href="base.html#显式使用包函数时-与-的区别"><i class="fa fa-check"></i><b>2.6.3</b> 显式使用包函数时 :: 与 ::: 的区别</a></li>
<li class="chapter" data-level="2.6.4" data-path="base.html"><a href="base.html#因子重构"><i class="fa fa-check"></i><b>2.6.4</b> 因子重构</a></li>
<li class="chapter" data-level="2.6.5" data-path="base.html"><a href="base.html#理解-r-计算"><i class="fa fa-check"></i><b>2.6.5</b> 理解 R 计算</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="import.html"><a href="import.html"><i class="fa fa-check"></i><b>3</b> 数据导入</a><ul>
<li class="chapter" data-level="3.1" data-path="import.html"><a href="import.html#符号分隔文件"><i class="fa fa-check"></i><b>3.1</b> 符号分隔文件</a><ul>
<li class="chapter" data-level="3.1.1" data-path="import.html"><a href="import.html#csv"><i class="fa fa-check"></i><b>3.1.1</b> CSV</a></li>
<li class="chapter" data-level="3.1.2" data-path="import.html"><a href="import.html#tsv-与其他-csv-变体"><i class="fa fa-check"></i><b>3.1.2</b> TSV 与其他 CSV 变体</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="import.html"><a href="import.html#excel"><i class="fa fa-check"></i><b>3.2</b> Excel</a></li>
<li class="chapter" data-level="3.3" data-path="import.html"><a href="import.html#json"><i class="fa fa-check"></i><b>3.3</b> JSON</a></li>
<li class="chapter" data-level="3.4" data-path="import.html"><a href="import.html#r-数据文件"><i class="fa fa-check"></i><b>3.4</b> R 数据文件</a><ul>
<li class="chapter" data-level="3.4.1" data-path="import.html"><a href="import.html#rdata"><i class="fa fa-check"></i><b>3.4.1</b> RData</a></li>
<li class="chapter" data-level="3.4.2" data-path="import.html"><a href="import.html#rds"><i class="fa fa-check"></i><b>3.4.2</b> RDS</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="import.html"><a href="import.html#常见问题与方案-2"><i class="fa fa-check"></i><b>3.5</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="3.5.1" data-path="import.html"><a href="import.html#通过键盘和剪贴板载入数据"><i class="fa fa-check"></i><b>3.5.1</b> 通过键盘和剪贴板载入数据</a></li>
<li class="chapter" data-level="3.5.2" data-path="import.html"><a href="import.html#逐行读取数据"><i class="fa fa-check"></i><b>3.5.2</b> 逐行读取数据</a></li>
<li class="chapter" data-level="3.5.3" data-path="import.html"><a href="import.html#读取等宽格式数据"><i class="fa fa-check"></i><b>3.5.3</b> 读取等宽格式数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="clean.html"><a href="clean.html"><i class="fa fa-check"></i><b>4</b> 数据清洗</a><ul>
<li class="chapter" data-level="4.1" data-path="clean.html"><a href="clean.html#向量-1"><i class="fa fa-check"></i><b>4.1</b> 向量</a></li>
<li class="chapter" data-level="4.2" data-path="clean.html"><a href="clean.html#单个数据集"><i class="fa fa-check"></i><b>4.2</b> 单个数据集</a><ul>
<li class="chapter" data-level="4.2.1" data-path="clean.html"><a href="clean.html#选择列"><i class="fa fa-check"></i><b>4.2.1</b> 选择列</a></li>
<li class="chapter" data-level="4.2.2" data-path="clean.html"><a href="clean.html#过滤行"><i class="fa fa-check"></i><b>4.2.2</b> 过滤行</a></li>
<li class="chapter" data-level="4.2.3" data-path="clean.html"><a href="clean.html#新增列"><i class="fa fa-check"></i><b>4.2.3</b> 新增列</a></li>
<li class="chapter" data-level="4.2.4" data-path="clean.html"><a href="clean.html#聚合运算"><i class="fa fa-check"></i><b>4.2.4</b> 聚合运算</a></li>
<li class="chapter" data-level="4.2.5" data-path="clean.html"><a href="clean.html#分组计算"><i class="fa fa-check"></i><b>4.2.5</b> 分组计算</a></li>
<li class="chapter" data-level="4.2.6" data-path="clean.html"><a href="clean.html#缺失值填充"><i class="fa fa-check"></i><b>4.2.6</b> 缺失值填充</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="clean.html"><a href="clean.html#两个数据集"><i class="fa fa-check"></i><b>4.3</b> 两个数据集</a></li>
<li class="chapter" data-level="4.4" data-path="clean.html"><a href="clean.html#多个数据集"><i class="fa fa-check"></i><b>4.4</b> 多个数据集</a></li>
<li class="chapter" data-level="4.5" data-path="clean.html"><a href="clean.html#常见问题与方案-3"><i class="fa fa-check"></i><b>4.5</b> 常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="visualization.html"><a href="visualization.html"><i class="fa fa-check"></i><b>5</b> 数据可视化</a><ul>
<li class="chapter" data-level="" data-path="visualization.html"><a href="visualization.html#常见问题与方案-4"><i class="fa fa-check"></i>常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="model.html"><a href="model.html"><i class="fa fa-check"></i><b>6</b> 统计建模</a><ul>
<li class="chapter" data-level="" data-path="model.html"><a href="model.html#常见问题与方案-5"><i class="fa fa-check"></i>常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="report.html"><a href="report.html"><i class="fa fa-check"></i><b>7</b> 结果展示</a><ul>
<li class="chapter" data-level="7.1" data-path="report.html"><a href="report.html#图形"><i class="fa fa-check"></i><b>7.1</b> 图形</a></li>
<li class="chapter" data-level="7.2" data-path="report.html"><a href="report.html#表格"><i class="fa fa-check"></i><b>7.2</b> 表格</a><ul>
<li class="chapter" data-level="7.2.1" data-path="report.html"><a href="report.html#excel-1"><i class="fa fa-check"></i><b>7.2.1</b> Excel</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="report.html"><a href="report.html#rmarkdown"><i class="fa fa-check"></i><b>7.3</b> RMarkdown</a></li>
<li class="chapter" data-level="7.4" data-path="report.html"><a href="report.html#shiny"><i class="fa fa-check"></i><b>7.4</b> Shiny</a></li>
<li class="chapter" data-level="7.5" data-path="report.html"><a href="report.html#常见问题与方案-6"><i class="fa fa-check"></i><b>7.5</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="7.5.1" data-path="report.html"><a href="report.html#export-包集才华与一身"><i class="fa fa-check"></i><b>7.5.1</b> export 包：集才华与一身</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="bioapp.html"><a href="bioapp.html"><i class="fa fa-check"></i><b>8</b> 生物信息学应用</a></li>
<li class="appendix"><span><b>附录</b></span></li>
<li class="chapter" data-level="A" data-path="search-table.html"><a href="search-table.html"><i class="fa fa-check"></i><b>A</b> 速查表</a><ul>
<li class="chapter" data-level="A.1" data-path="search-table.html"><a href="search-table.html#数值计算函数"><i class="fa fa-check"></i><b>A.1</b> 数值计算函数</a></li>
<li class="chapter" data-level="A.2" data-path="search-table.html"><a href="search-table.html#模型符号与函数"><i class="fa fa-check"></i><b>A.2</b> 模型符号与函数</a></li>
<li class="chapter" data-level="A.3" data-path="search-table.html"><a href="search-table.html#统计分析与检验"><i class="fa fa-check"></i><b>A.3</b> 统计分析与检验</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="expand-reading.html"><a href="expand-reading.html"><i class="fa fa-check"></i><b>B</b> 拓展阅读</a><ul>
<li class="chapter" data-level="B.1" data-path="expand-reading.html"><a href="expand-reading.html#生信技能树语雀知识库"><i class="fa fa-check"></i><b>B.1</b> 生信技能树语雀知识库</a></li>
<li class="chapter" data-level="B.2" data-path="expand-reading.html"><a href="expand-reading.html#图书"><i class="fa fa-check"></i><b>B.2</b> 图书</a><ul>
<li class="chapter" data-level="B.2.1" data-path="expand-reading.html"><a href="expand-reading.html#问题与方案"><i class="fa fa-check"></i><b>B.2.1</b> 问题与方案</a></li>
<li class="chapter" data-level="B.2.2" data-path="expand-reading.html"><a href="expand-reading.html#统计建模"><i class="fa fa-check"></i><b>B.2.2</b> 统计建模</a></li>
<li class="chapter" data-level="B.2.3" data-path="expand-reading.html"><a href="expand-reading.html#核心集合"><i class="fa fa-check"></i><b>B.2.3</b> 核心集合</a></li>
<li class="chapter" data-level="B.2.4" data-path="expand-reading.html"><a href="expand-reading.html#生物信息学"><i class="fa fa-check"></i><b>B.2.4</b> 生物信息学</a></li>
<li class="chapter" data-level="B.2.5" data-path="expand-reading.html"><a href="expand-reading.html#r"><i class="fa fa-check"></i><b>B.2.5</b> R</a></li>
</ul></li>
<li class="chapter" data-level="B.3" data-path="expand-reading.html"><a href="expand-reading.html#视频"><i class="fa fa-check"></i><b>B.3</b> 视频</a></li>
<li class="chapter" data-level="B.4" data-path="expand-reading.html"><a href="expand-reading.html#公众号"><i class="fa fa-check"></i><b>B.4</b> 公众号</a></li>
<li class="chapter" data-level="B.5" data-path="expand-reading.html"><a href="expand-reading.html#其他资料"><i class="fa fa-check"></i><b>B.5</b> 其他资料</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>参考文献</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">极客R：数据分析之道</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="import" class="section level1">
<h1><span class="header-section-number">第 3 章</span> 数据导入</h1>
<p>在掌握一定的 R 编程技能后，我们开始迈入数据分析的殿堂。大部分数据分析事务的数据都不是通过 R 创建，而是来自于各种数据收集软硬件、渠道，包括 Excel、网络等。本章聚焦于如何将数据导入 R 中以便于开始数据分析。对于本章的内容，读者既可以系统地从头到尾深入阅读学习，也可以根据自己实际工作需要或时间限制选择一些重点或感兴趣内容进行掌握。</p>
<p>本章的重点内容包括符号分隔文件、Excel 文件、JSON 文件以及 R 支持的数据格式 RData 和 RDS，其他格式的数据将放到本章【<strong>常见问题与方案</strong>】一节作为补充介绍。</p>
<div id="符号分隔文件" class="section level2">
<h2><span class="header-section-number">3.1</span> 符号分隔文件</h2>
<p>符号分隔文件是<strong>最最常用</strong>的数据文件格式，知道如何导入它是读者的必备技能。这里的<strong>符号</strong>泛指一切可以用作数据内容分隔的符号，常见的包括逗号（<code>,</code>），制表符（<code>\t</code>），我们常称它们为 CSV 文件和 TSV 文件。</p>
<div id="csv" class="section level3">
<h3><span class="header-section-number">3.1.1</span> CSV</h3>
<p>CSV 文件常以 <code>.csv</code> 作为文件拓展名，比如接下来我们会导入的 <code>mtcars.csv</code>。注意，文件拓展名并不会影响文件本身的内容，它只是用来方便帮助人们快速的了解内容格式，另外支持其他一些程序的自动解读（在你的计算机上，不同的文件拓展名系统软件可以会对它们使用不同的图标，如 Word 文档和 PPT）。</p>
<p>一般规整的数据以行作为观测，列作为记录（变量、域），如一个班级同学的成绩。</p>
<pre><code>student,chinese,math,english
stu1,99,100,98
stu2,60,50,88</code></pre>
<p>R 内置了 <code>read.table()</code> 函数用于便捷导入各类分隔符的文件。下面我们直接将这个成绩记录信息以文本的形式传入，结果会返回一个数据框：</p>
<div class="sourceCode" id="cb176"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb176-1" data-line-number="1">stu &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="dt">text =</span> <span class="st">&quot;</span></a>
<a class="sourceLine" id="cb176-2" data-line-number="2"><span class="st">student,chinese,math,english</span></a>
<a class="sourceLine" id="cb176-3" data-line-number="3"><span class="st">stu1,99,100,98</span></a>
<a class="sourceLine" id="cb176-4" data-line-number="4"><span class="st">stu2,60,50,88</span></a>
<a class="sourceLine" id="cb176-5" data-line-number="5"><span class="st">&quot;</span>, <span class="dt">header =</span> <span class="ot">TRUE</span>, <span class="dt">sep =</span> <span class="st">&quot;,&quot;</span>)</a>
<a class="sourceLine" id="cb176-6" data-line-number="6">stu</a>
<a class="sourceLine" id="cb176-7" data-line-number="7"><span class="co">#&gt;   student chinese math english</span></a>
<a class="sourceLine" id="cb176-8" data-line-number="8"><span class="co">#&gt; 1    stu1      99  100      98</span></a>
<a class="sourceLine" id="cb176-9" data-line-number="9"><span class="co">#&gt; 2    stu2      60   50      88</span></a>
<a class="sourceLine" id="cb176-10" data-line-number="10"><span class="kw">class</span>(stu)</a>
<a class="sourceLine" id="cb176-11" data-line-number="11"><span class="co">#&gt; [1] &quot;data.frame&quot;</span></a></code></pre></div>
<p>实际上要处理的数据并不会这么的少，一般会以文件的形式存储在计算机磁盘中。下面我们依旧使用 <code>read.table()</code> 函数完成 CSV 文件数据的导入。</p>
<div class="sourceCode" id="cb177"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb177-1" data-line-number="1">cars &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="dt">file =</span> <span class="st">&quot;data/data-import/mtcars.csv&quot;</span>, <span class="dt">header =</span> <span class="ot">TRUE</span>, <span class="dt">sep =</span> <span class="st">&quot;,&quot;</span>)</a></code></pre></div>
<p>操作完成了，我们检查下头几行：</p>
<div class="sourceCode" id="cb178"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb178-1" data-line-number="1"><span class="kw">head</span>(cars)</a>
<a class="sourceLine" id="cb178-2" data-line-number="2"><span class="co">#&gt;    mpg cyl disp  hp drat    wt  qsec vs am gear carb</span></a>
<a class="sourceLine" id="cb178-3" data-line-number="3"><span class="co">#&gt; 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb178-4" data-line-number="4"><span class="co">#&gt; 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb178-5" data-line-number="5"><span class="co">#&gt; 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb178-6" data-line-number="6"><span class="co">#&gt; 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb178-7" data-line-number="7"><span class="co">#&gt; 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb178-8" data-line-number="8"><span class="co">#&gt; 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</span></a></code></pre></div>
<p>除了使用 <code>read.table()</code>，我们还可以使用内置的 <code>read.csv()</code> 函数完成 CSV 文件的读入。与 <code>read.table()</code> 不同的时，我们无需再指定分隔符，因为该函数本身就是为了 CSV 文件设计的。另外函数默认将 <code>header</code> 选项设定为 <code>TRUE</code>，即有列名，所以我们也无需指定它，因而读取操作就被简化了：</p>
<div class="sourceCode" id="cb179"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb179-1" data-line-number="1">cars2 &lt;-<span class="st"> </span><span class="kw">read.csv</span>(<span class="dt">file =</span> <span class="st">&quot;data/data-import/mtcars.csv&quot;</span>)</a>
<a class="sourceLine" id="cb179-2" data-line-number="2"><span class="kw">head</span>(cars2)</a>
<a class="sourceLine" id="cb179-3" data-line-number="3"><span class="co">#&gt;    mpg cyl disp  hp drat    wt  qsec vs am gear carb</span></a>
<a class="sourceLine" id="cb179-4" data-line-number="4"><span class="co">#&gt; 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb179-5" data-line-number="5"><span class="co">#&gt; 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb179-6" data-line-number="6"><span class="co">#&gt; 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb179-7" data-line-number="7"><span class="co">#&gt; 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb179-8" data-line-number="8"><span class="co">#&gt; 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb179-9" data-line-number="9"><span class="co">#&gt; 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</span></a></code></pre></div>
<p>上述的读取操作基于 R 内置的函数实现，无需载入任何其他三方包就可以完成数据的读入，这在针对小型数据（集）或者计算机条件受限时（无法安装和使用三方包）非常有用。在这种常规以符号分隔的文件数据读取方面，我必须提及 2 个三方包：<strong>readr</strong> 和 <strong>data.table</strong>。它们都能以极快的速度读取大内存数据，推荐读者作为常规导入操作的解决方案。</p>
<p><a href="https://github.com/tidyverse/"><strong>tidyverse</strong></a> 是 R 语言大神 <a href="https://github.com/hadley">Hadley</a>（以 <strong>ggplot2</strong> 作者闻名于世） 组织构建的一整套数据分析生态系统，包括读入、处理、建模与可视化等， <a href="https://github.com/tidyverse/readr"><strong>readr</strong></a> 包是 <strong>tidyverse</strong> 的一部分，用于完成数据的导入工作。</p>
<p><a href="https://github.com/Rdatatable/data.table"><strong>data.table</strong></a> 包以 R 社区最快的数据读取和处理操作而著名，它主要是提供了一个增强版的数据框 <code>data.table</code>。</p>
<p>根据 <strong>readr</strong> 包官方文档介绍，<strong>readr</strong> 包通常比 <strong>data.table</strong> 包慢大概 1.2~2 倍左右。不过它们各有特点，<strong>readr</strong> 包被设计用于更为常规的数据读取操作，而 <strong>data.table</strong> 的目标则是尽量的快。</p>
<p>为了体现上述两个包和内置函数的差别，这里我们构造一个较大的 CSV 文件进行简单的测试：</p>
<div class="sourceCode" id="cb180"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb180-1" data-line-number="1">huge_car &lt;-<span class="st"> </span>cars[<span class="kw">rep</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">32</span>, <span class="dv">10000</span>), ]</a></code></pre></div>
<p>把这个数据先保存下来，然后再利用不同的工具进行导入。</p>
<div class="sourceCode" id="cb181"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb181-1" data-line-number="1">temp_csv &lt;-<span class="st"> </span><span class="kw">tempfile</span>(<span class="dt">fileext =</span> <span class="st">&quot;.csv&quot;</span>)</a>
<a class="sourceLine" id="cb181-2" data-line-number="2">readr<span class="op">::</span><span class="kw">write_csv</span>(huge_car, <span class="dt">path =</span> temp_csv)</a></code></pre></div>
<p>现在我们分别使用 <code>system.time()</code> 测试下 R 内置的 <code>read.csv()</code> 函数与 <strong>readr</strong> 提供的 <code>read_csv()</code> 以及 <strong>data.table</strong> 提供的 <code>fread()</code> 的读取效率。</p>
<div class="sourceCode" id="cb182"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb182-1" data-line-number="1">time1 &lt;-<span class="st"> </span><span class="kw">system.time</span>(</a>
<a class="sourceLine" id="cb182-2" data-line-number="2">  z1 &lt;-<span class="st"> </span><span class="kw">read.csv</span>(temp_csv)</a>
<a class="sourceLine" id="cb182-3" data-line-number="3">)</a>
<a class="sourceLine" id="cb182-4" data-line-number="4">time1</a>
<a class="sourceLine" id="cb182-5" data-line-number="5"><span class="co">#&gt;  用户  系统  流逝 </span></a>
<a class="sourceLine" id="cb182-6" data-line-number="6"><span class="co">#&gt; 0.896 0.021 0.917</span></a></code></pre></div>
<div class="sourceCode" id="cb183"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb183-1" data-line-number="1"><span class="kw">library</span>(readr)</a>
<a class="sourceLine" id="cb183-2" data-line-number="2"></a>
<a class="sourceLine" id="cb183-3" data-line-number="3">time2 &lt;-<span class="st"> </span><span class="kw">system.time</span>(</a>
<a class="sourceLine" id="cb183-4" data-line-number="4">  z2 &lt;-<span class="st">  </span><span class="kw">read_csv</span>(temp_csv)</a>
<a class="sourceLine" id="cb183-5" data-line-number="5">)</a>
<a class="sourceLine" id="cb183-6" data-line-number="6">time2</a>
<a class="sourceLine" id="cb183-7" data-line-number="7"><span class="co">#&gt;  用户  系统  流逝 </span></a>
<a class="sourceLine" id="cb183-8" data-line-number="8"><span class="co">#&gt; 0.287 0.008 0.294</span></a></code></pre></div>
<div class="sourceCode" id="cb184"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb184-1" data-line-number="1"><span class="kw">library</span>(data.table)</a>
<a class="sourceLine" id="cb184-2" data-line-number="2"></a>
<a class="sourceLine" id="cb184-3" data-line-number="3">time3 &lt;-<span class="st"> </span><span class="kw">system.time</span>(</a>
<a class="sourceLine" id="cb184-4" data-line-number="4">  z3 &lt;-<span class="st"> </span><span class="kw">fread</span>(temp_csv)</a>
<a class="sourceLine" id="cb184-5" data-line-number="5">)</a>
<a class="sourceLine" id="cb184-6" data-line-number="6">time3</a>
<a class="sourceLine" id="cb184-7" data-line-number="7"><span class="co">#&gt;  用户  系统  流逝 </span></a>
<a class="sourceLine" id="cb184-8" data-line-number="8"><span class="co">#&gt; 0.041 0.005 0.047</span></a></code></pre></div>
<p>上面我们使用了 320000 行数据进行测试，在我的计算机上，内置的函数 <code>read.csv()</code> 总共花费了 0.917s，<strong>readr</strong> 的 <code>read_csv()</code> 花费了 0.294s，而 <strong>data.table</strong> 的 <code>fread()</code> 仅用了 0.047s 左右。<strong>好的、适合的工具可以帮助我们极大地提升工作效率</strong>。</p>
<p>如果我们进一步观察几种不同方式导入的数据格式，就会发现它们有些不太相同。</p>
<div class="sourceCode" id="cb185"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb185-1" data-line-number="1">z1</a>
<a class="sourceLine" id="cb185-2" data-line-number="2"><span class="co">#&gt;     mpg cyl  disp  hp drat    wt  qsec vs am gear carb</span></a>
<a class="sourceLine" id="cb185-3" data-line-number="3"><span class="co">#&gt; 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-4" data-line-number="4"><span class="co">#&gt; 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-5" data-line-number="5"><span class="co">#&gt; 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-6" data-line-number="6"><span class="co">#&gt; 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-7" data-line-number="7"><span class="co">#&gt; 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-8" data-line-number="8"><span class="co">#&gt; 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-9" data-line-number="9"><span class="co">#&gt; 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-10" data-line-number="10"><span class="co">#&gt; 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-11" data-line-number="11"><span class="co">#&gt; 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-12" data-line-number="12"><span class="co">#&gt; 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-13" data-line-number="13"><span class="co">#&gt; 11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-14" data-line-number="14"><span class="co">#&gt; 12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-15" data-line-number="15"><span class="co">#&gt; 13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-16" data-line-number="16"><span class="co">#&gt; 14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-17" data-line-number="17"><span class="co">#&gt; 15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-18" data-line-number="18"><span class="co">#&gt; 16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-19" data-line-number="19"><span class="co">#&gt; 17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-20" data-line-number="20"><span class="co">#&gt; 18 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-21" data-line-number="21"><span class="co">#&gt; 19 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2</span></a>
<a class="sourceLine" id="cb185-22" data-line-number="22"><span class="co">#&gt; 20 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-23" data-line-number="23"><span class="co">#&gt; 21 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-24" data-line-number="24"><span class="co">#&gt; 22 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-25" data-line-number="25"><span class="co">#&gt; 23 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-26" data-line-number="26"><span class="co">#&gt; 24 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-27" data-line-number="27"><span class="co">#&gt; 25 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-28" data-line-number="28"><span class="co">#&gt; 26 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-29" data-line-number="29"><span class="co">#&gt; 27 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2</span></a>
<a class="sourceLine" id="cb185-30" data-line-number="30"><span class="co">#&gt; 28 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2</span></a>
<a class="sourceLine" id="cb185-31" data-line-number="31"><span class="co">#&gt; 29 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4</span></a>
<a class="sourceLine" id="cb185-32" data-line-number="32"><span class="co">#&gt; 30 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6</span></a>
<a class="sourceLine" id="cb185-33" data-line-number="33"><span class="co">#&gt; 31 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8</span></a>
<a class="sourceLine" id="cb185-34" data-line-number="34"><span class="co">#&gt; 32 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2</span></a>
<a class="sourceLine" id="cb185-35" data-line-number="35"><span class="co">#&gt; 33 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-36" data-line-number="36"><span class="co">#&gt; 34 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-37" data-line-number="37"><span class="co">#&gt; 35 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-38" data-line-number="38"><span class="co">#&gt; 36 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-39" data-line-number="39"><span class="co">#&gt; 37 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-40" data-line-number="40"><span class="co">#&gt; 38 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-41" data-line-number="41"><span class="co">#&gt; 39 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-42" data-line-number="42"><span class="co">#&gt; 40 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-43" data-line-number="43"><span class="co">#&gt; 41 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-44" data-line-number="44"><span class="co">#&gt; 42 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-45" data-line-number="45"><span class="co">#&gt; 43 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-46" data-line-number="46"><span class="co">#&gt; 44 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-47" data-line-number="47"><span class="co">#&gt; 45 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-48" data-line-number="48"><span class="co">#&gt; 46 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-49" data-line-number="49"><span class="co">#&gt; 47 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-50" data-line-number="50"><span class="co">#&gt; 48 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-51" data-line-number="51"><span class="co">#&gt; 49 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-52" data-line-number="52"><span class="co">#&gt; 50 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-53" data-line-number="53"><span class="co">#&gt; 51 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2</span></a>
<a class="sourceLine" id="cb185-54" data-line-number="54"><span class="co">#&gt; 52 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-55" data-line-number="55"><span class="co">#&gt; 53 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-56" data-line-number="56"><span class="co">#&gt; 54 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-57" data-line-number="57"><span class="co">#&gt; 55 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-58" data-line-number="58"><span class="co">#&gt; 56 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-59" data-line-number="59"><span class="co">#&gt; 57 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-60" data-line-number="60"><span class="co">#&gt; 58 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-61" data-line-number="61"><span class="co">#&gt; 59 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2</span></a>
<a class="sourceLine" id="cb185-62" data-line-number="62"><span class="co">#&gt; 60 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2</span></a>
<a class="sourceLine" id="cb185-63" data-line-number="63"><span class="co">#&gt; 61 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4</span></a>
<a class="sourceLine" id="cb185-64" data-line-number="64"><span class="co">#&gt; 62 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6</span></a>
<a class="sourceLine" id="cb185-65" data-line-number="65"><span class="co">#&gt; 63 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8</span></a>
<a class="sourceLine" id="cb185-66" data-line-number="66"><span class="co">#&gt; 64 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2</span></a>
<a class="sourceLine" id="cb185-67" data-line-number="67"><span class="co">#&gt; 65 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-68" data-line-number="68"><span class="co">#&gt; 66 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb185-69" data-line-number="69"><span class="co">#&gt; 67 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-70" data-line-number="70"><span class="co">#&gt; 68 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-71" data-line-number="71"><span class="co">#&gt; 69 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-72" data-line-number="72"><span class="co">#&gt; 70 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-73" data-line-number="73"><span class="co">#&gt; 71 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-74" data-line-number="74"><span class="co">#&gt; 72 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-75" data-line-number="75"><span class="co">#&gt; 73 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2</span></a>
<a class="sourceLine" id="cb185-76" data-line-number="76"><span class="co">#&gt; 74 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-77" data-line-number="77"><span class="co">#&gt; 75 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4</span></a>
<a class="sourceLine" id="cb185-78" data-line-number="78"><span class="co">#&gt; 76 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-79" data-line-number="79"><span class="co">#&gt; 77 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-80" data-line-number="80"><span class="co">#&gt; 78 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3</span></a>
<a class="sourceLine" id="cb185-81" data-line-number="81"><span class="co">#&gt; 79 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-82" data-line-number="82"><span class="co">#&gt; 80 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-83" data-line-number="83"><span class="co">#&gt; 81 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-84" data-line-number="84"><span class="co">#&gt; 82 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-85" data-line-number="85"><span class="co">#&gt; 83 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2</span></a>
<a class="sourceLine" id="cb185-86" data-line-number="86"><span class="co">#&gt; 84 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-87" data-line-number="87"><span class="co">#&gt; 85 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1</span></a>
<a class="sourceLine" id="cb185-88" data-line-number="88"><span class="co">#&gt; 86 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-89" data-line-number="89"><span class="co">#&gt; 87 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-90" data-line-number="90"><span class="co">#&gt; 88 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4</span></a>
<a class="sourceLine" id="cb185-91" data-line-number="91"><span class="co">#&gt; 89 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2</span></a>
<a class="sourceLine" id="cb185-92" data-line-number="92"><span class="co">#&gt; 90 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb185-93" data-line-number="93"><span class="co">#&gt;  [ reached &#39;max&#39; / getOption(&quot;max.print&quot;) -- omitted 319910 rows ]</span></a>
<a class="sourceLine" id="cb185-94" data-line-number="94">z2</a>
<a class="sourceLine" id="cb185-95" data-line-number="95"><span class="co">#&gt; # A tibble: 320,000 x 11</span></a>
<a class="sourceLine" id="cb185-96" data-line-number="96"><span class="co">#&gt;      mpg   cyl  disp    hp  drat    wt  qsec    vs</span></a>
<a class="sourceLine" id="cb185-97" data-line-number="97"><span class="co">#&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></a>
<a class="sourceLine" id="cb185-98" data-line-number="98"><span class="co">#&gt;  1  21       6  160    110  3.9   2.62  16.5     0</span></a>
<a class="sourceLine" id="cb185-99" data-line-number="99"><span class="co">#&gt;  2  21       6  160    110  3.9   2.88  17.0     0</span></a>
<a class="sourceLine" id="cb185-100" data-line-number="100"><span class="co">#&gt;  3  22.8     4  108     93  3.85  2.32  18.6     1</span></a>
<a class="sourceLine" id="cb185-101" data-line-number="101"><span class="co">#&gt;  4  21.4     6  258    110  3.08  3.22  19.4     1</span></a>
<a class="sourceLine" id="cb185-102" data-line-number="102"><span class="co">#&gt;  5  18.7     8  360    175  3.15  3.44  17.0     0</span></a>
<a class="sourceLine" id="cb185-103" data-line-number="103"><span class="co">#&gt;  6  18.1     6  225    105  2.76  3.46  20.2     1</span></a>
<a class="sourceLine" id="cb185-104" data-line-number="104"><span class="co">#&gt;  7  14.3     8  360    245  3.21  3.57  15.8     0</span></a>
<a class="sourceLine" id="cb185-105" data-line-number="105"><span class="co">#&gt;  8  24.4     4  147.    62  3.69  3.19  20       1</span></a>
<a class="sourceLine" id="cb185-106" data-line-number="106"><span class="co">#&gt;  9  22.8     4  141.    95  3.92  3.15  22.9     1</span></a>
<a class="sourceLine" id="cb185-107" data-line-number="107"><span class="co">#&gt; 10  19.2     6  168.   123  3.92  3.44  18.3     1</span></a>
<a class="sourceLine" id="cb185-108" data-line-number="108"><span class="co">#&gt; # … with 319,990 more rows, and 3 more variables:</span></a>
<a class="sourceLine" id="cb185-109" data-line-number="109"><span class="co">#&gt; #   am &lt;dbl&gt;, gear &lt;dbl&gt;, carb &lt;dbl&gt;</span></a>
<a class="sourceLine" id="cb185-110" data-line-number="110">z3</a>
<a class="sourceLine" id="cb185-111" data-line-number="111"><span class="co">#&gt;          mpg cyl  disp  hp drat    wt  qsec vs am gear</span></a>
<a class="sourceLine" id="cb185-112" data-line-number="112"><span class="co">#&gt;      1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4</span></a>
<a class="sourceLine" id="cb185-113" data-line-number="113"><span class="co">#&gt;      2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4</span></a>
<a class="sourceLine" id="cb185-114" data-line-number="114"><span class="co">#&gt;      3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4</span></a>
<a class="sourceLine" id="cb185-115" data-line-number="115"><span class="co">#&gt;      4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3</span></a>
<a class="sourceLine" id="cb185-116" data-line-number="116"><span class="co">#&gt;      5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3</span></a>
<a class="sourceLine" id="cb185-117" data-line-number="117"><span class="co">#&gt;     ---                                               </span></a>
<a class="sourceLine" id="cb185-118" data-line-number="118"><span class="co">#&gt; 319996: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5</span></a>
<a class="sourceLine" id="cb185-119" data-line-number="119"><span class="co">#&gt; 319997: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5</span></a>
<a class="sourceLine" id="cb185-120" data-line-number="120"><span class="co">#&gt; 319998: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5</span></a>
<a class="sourceLine" id="cb185-121" data-line-number="121"><span class="co">#&gt; 319999: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5</span></a>
<a class="sourceLine" id="cb185-122" data-line-number="122"><span class="co">#&gt; 320000: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4</span></a>
<a class="sourceLine" id="cb185-123" data-line-number="123"><span class="co">#&gt;         carb</span></a>
<a class="sourceLine" id="cb185-124" data-line-number="124"><span class="co">#&gt;      1:    4</span></a>
<a class="sourceLine" id="cb185-125" data-line-number="125"><span class="co">#&gt;      2:    4</span></a>
<a class="sourceLine" id="cb185-126" data-line-number="126"><span class="co">#&gt;      3:    1</span></a>
<a class="sourceLine" id="cb185-127" data-line-number="127"><span class="co">#&gt;      4:    1</span></a>
<a class="sourceLine" id="cb185-128" data-line-number="128"><span class="co">#&gt;      5:    2</span></a>
<a class="sourceLine" id="cb185-129" data-line-number="129"><span class="co">#&gt;     ---     </span></a>
<a class="sourceLine" id="cb185-130" data-line-number="130"><span class="co">#&gt; 319996:    2</span></a>
<a class="sourceLine" id="cb185-131" data-line-number="131"><span class="co">#&gt; 319997:    4</span></a>
<a class="sourceLine" id="cb185-132" data-line-number="132"><span class="co">#&gt; 319998:    6</span></a>
<a class="sourceLine" id="cb185-133" data-line-number="133"><span class="co">#&gt; 319999:    8</span></a>
<a class="sourceLine" id="cb185-134" data-line-number="134"><span class="co">#&gt; 320000:    2</span></a></code></pre></div>
<p>这个所谓的不同与结构中存储的数据信息无关，而是在不同的设计上。我们检查一下它们的类属性：</p>
<div class="sourceCode" id="cb186"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb186-1" data-line-number="1"><span class="kw">class</span>(z1)</a>
<a class="sourceLine" id="cb186-2" data-line-number="2"><span class="co">#&gt; [1] &quot;data.frame&quot;</span></a>
<a class="sourceLine" id="cb186-3" data-line-number="3"><span class="kw">class</span>(z2)</a>
<a class="sourceLine" id="cb186-4" data-line-number="4"><span class="co">#&gt; [1] &quot;spec_tbl_df&quot; &quot;tbl_df&quot;      &quot;tbl&quot;        </span></a>
<a class="sourceLine" id="cb186-5" data-line-number="5"><span class="co">#&gt; [4] &quot;data.frame&quot;</span></a>
<a class="sourceLine" id="cb186-6" data-line-number="6"><span class="kw">class</span>(z3)</a>
<a class="sourceLine" id="cb186-7" data-line-number="7"><span class="co">#&gt; [1] &quot;data.table&quot; &quot;data.frame&quot;</span></a></code></pre></div>
<p>不难看到这 3 个对象存在共有的类名 <code>data.frame</code>。我们使用内置函数读入数据仅包含该类名，而后两者还存在其他的类名，这是因为后两者继承了 <code>data.frame</code>。简单地说，后两者是增强版的 <code>data.frame</code>，它们赋予了不同于 R 内置数据框的特性，读者可以观察到的最明显的区别就是它们打印信息的方式不同。</p>
<p>通常地说，我们将对象 <code>z2</code> 称为 <code>tibble</code>，因为它是由 <a href="https://github.com/tidyverse/tibble"><strong>tibble</strong></a> 包创建的类，是 <strong>tidyverse</strong> 系列包的数据结构基础，设计者称它为现代的 <code>data.frame</code>，在基础的使用方式上与 <code>data.frame</code> 并无不同，读者可以通过官方文档阅读更为详细的介绍。</p>
<p>对象 <code>z3</code> 则常被称为 <code>data.table</code>，因为它的类名和包名都是 <code>data.table</code>.</p>
<p><code>tibble</code> 和 <code>data.table</code> 都有一系列强大高效的数据处理方法和操作，它们将在第 <a href="clean.html#clean">4</a> 章进行介绍。</p>
</div>
<div id="tsv-与其他-csv-变体" class="section level3">
<h3><span class="header-section-number">3.1.2</span> TSV 与其他 CSV 变体</h3>
<p>另一种流行的数据存储格式是 TSV，与 CSV 唯一不同的是 TSV 使用制表符 <code>\t</code> 作为内容的分隔符。TSV 文件除了以 <code>.tsv</code> 作为文件拓展名，也常用 <code>.txt</code> 作为文件拓展名（并不是所有的 <code>.txt</code> 文件都是以制表符分隔）。</p>
<p>通过将 <code>read.table()</code> 函数中的 <code>sep</code> 参数设定为制表符，我们可以轻松完成该格式文件内容的读取：</p>
<div class="sourceCode" id="cb187"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb187-1" data-line-number="1">mt &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="st">&quot;data/data-import/mtcars.tsv&quot;</span>, <span class="dt">sep =</span> <span class="st">&quot;</span><span class="ch">\t</span><span class="st">&quot;</span>, <span class="dt">header =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb187-2" data-line-number="2">mt</a>
<a class="sourceLine" id="cb187-3" data-line-number="3"><span class="co">#&gt;     mpg cyl  disp  hp drat    wt  qsec vs am gear carb</span></a>
<a class="sourceLine" id="cb187-4" data-line-number="4"><span class="co">#&gt; 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4</span></a>
<a class="sourceLine" id="cb187-5" data-line-number="5"><span class="co">#&gt; 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4</span></a>
<a class="sourceLine" id="cb187-6" data-line-number="6"><span class="co">#&gt; 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1</span></a>
<a class="sourceLine" id="cb187-7" data-line-number="7"><span class="co">#&gt; 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1</span></a>
<a class="sourceLine" id="cb187-8" data-line-number="8"><span class="co">#&gt; 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2</span></a>
<a class="sourceLine" id="cb187-9" data-line-number="9"><span class="co">#&gt; 6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1</span></a>
<a class="sourceLine" id="cb187-10" data-line-number="10"><span class="co">#&gt; 7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4</span></a>
<a class="sourceLine" id="cb187-11" data-line-number="11"><span class="co">#&gt; 8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2</span></a>
<a class="sourceLine" id="cb187-12" data-line-number="12"><span class="co">#&gt; 9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2</span></a>
<a class="sourceLine" id="cb187-13" data-line-number="13"><span class="co">#&gt; 10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4</span></a>
<a class="sourceLine" id="cb187-14" data-line-number="14"><span class="co">#&gt; 11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4</span></a>
<a class="sourceLine" id="cb187-15" data-line-number="15"><span class="co">#&gt; 12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3</span></a>
<a class="sourceLine" id="cb187-16" data-line-number="16"><span class="co">#&gt; 13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3</span></a>
<a class="sourceLine" id="cb187-17" data-line-number="17"><span class="co">#&gt; 14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3</span></a>
<a class="sourceLine" id="cb187-18" data-line-number="18"><span class="co">#&gt; 15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4</span></a>
<a class="sourceLine" id="cb187-19" data-line-number="19"><span class="co">#&gt; 16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4</span></a>
<a class="sourceLine" id="cb187-20" data-line-number="20"><span class="co">#&gt; 17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4</span></a>
<a class="sourceLine" id="cb187-21" data-line-number="21"><span class="co">#&gt; 18 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1</span></a>
<a class="sourceLine" id="cb187-22" data-line-number="22"><span class="co">#&gt; 19 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2</span></a>
<a class="sourceLine" id="cb187-23" data-line-number="23"><span class="co">#&gt; 20 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb187-24" data-line-number="24"><span class="co">#&gt; 21 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1</span></a>
<a class="sourceLine" id="cb187-25" data-line-number="25"><span class="co">#&gt; 22 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2</span></a>
<a class="sourceLine" id="cb187-26" data-line-number="26"><span class="co">#&gt; 23 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2</span></a>
<a class="sourceLine" id="cb187-27" data-line-number="27"><span class="co">#&gt; 24 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4</span></a>
<a class="sourceLine" id="cb187-28" data-line-number="28"><span class="co">#&gt; 25 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2</span></a>
<a class="sourceLine" id="cb187-29" data-line-number="29"><span class="co">#&gt; 26 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1</span></a>
<a class="sourceLine" id="cb187-30" data-line-number="30"><span class="co">#&gt; 27 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2</span></a>
<a class="sourceLine" id="cb187-31" data-line-number="31"><span class="co">#&gt; 28 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2</span></a>
<a class="sourceLine" id="cb187-32" data-line-number="32"><span class="co">#&gt; 29 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4</span></a>
<a class="sourceLine" id="cb187-33" data-line-number="33"><span class="co">#&gt; 30 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6</span></a>
<a class="sourceLine" id="cb187-34" data-line-number="34"><span class="co">#&gt; 31 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8</span></a>
<a class="sourceLine" id="cb187-35" data-line-number="35"><span class="co">#&gt; 32 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2</span></a></code></pre></div>
<p>而 <strong>readr</strong> 包提供了一系列的 <code>read_*()</code> 函数，方便用户将常见数据文件导入为 <code>tibble</code>：</p>
<div class="sourceCode" id="cb188"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb188-1" data-line-number="1">mt2 &lt;-<span class="st"> </span><span class="kw">read_tsv</span>(<span class="st">&quot;data/data-import/mtcars.tsv&quot;</span>)</a>
<a class="sourceLine" id="cb188-2" data-line-number="2">mt2</a>
<a class="sourceLine" id="cb188-3" data-line-number="3"><span class="co">#&gt; # A tibble: 32 x 11</span></a>
<a class="sourceLine" id="cb188-4" data-line-number="4"><span class="co">#&gt;      mpg   cyl  disp    hp  drat    wt  qsec    vs</span></a>
<a class="sourceLine" id="cb188-5" data-line-number="5"><span class="co">#&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></a>
<a class="sourceLine" id="cb188-6" data-line-number="6"><span class="co">#&gt;  1  21       6  160    110  3.9   2.62  16.5     0</span></a>
<a class="sourceLine" id="cb188-7" data-line-number="7"><span class="co">#&gt;  2  21       6  160    110  3.9   2.88  17.0     0</span></a>
<a class="sourceLine" id="cb188-8" data-line-number="8"><span class="co">#&gt;  3  22.8     4  108     93  3.85  2.32  18.6     1</span></a>
<a class="sourceLine" id="cb188-9" data-line-number="9"><span class="co">#&gt;  4  21.4     6  258    110  3.08  3.22  19.4     1</span></a>
<a class="sourceLine" id="cb188-10" data-line-number="10"><span class="co">#&gt;  5  18.7     8  360    175  3.15  3.44  17.0     0</span></a>
<a class="sourceLine" id="cb188-11" data-line-number="11"><span class="co">#&gt;  6  18.1     6  225    105  2.76  3.46  20.2     1</span></a>
<a class="sourceLine" id="cb188-12" data-line-number="12"><span class="co">#&gt;  7  14.3     8  360    245  3.21  3.57  15.8     0</span></a>
<a class="sourceLine" id="cb188-13" data-line-number="13"><span class="co">#&gt;  8  24.4     4  147.    62  3.69  3.19  20       1</span></a>
<a class="sourceLine" id="cb188-14" data-line-number="14"><span class="co">#&gt;  9  22.8     4  141.    95  3.92  3.15  22.9     1</span></a>
<a class="sourceLine" id="cb188-15" data-line-number="15"><span class="co">#&gt; 10  19.2     6  168.   123  3.92  3.44  18.3     1</span></a>
<a class="sourceLine" id="cb188-16" data-line-number="16"><span class="co">#&gt; # … with 22 more rows, and 3 more variables:</span></a>
<a class="sourceLine" id="cb188-17" data-line-number="17"><span class="co">#&gt; #   am &lt;dbl&gt;, gear &lt;dbl&gt;, carb &lt;dbl&gt;</span></a></code></pre></div>
<p>使用 <strong>data.table</strong> 则更为轻松，因为所有分隔格式的数据都可以通过 <code>fread()</code> 读取完成：</p>
<div class="sourceCode" id="cb189"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb189-1" data-line-number="1">mt3 &lt;-<span class="st"> </span><span class="kw">fread</span>(<span class="st">&quot;data/data-import/mtcars.tsv&quot;</span>)</a>
<a class="sourceLine" id="cb189-2" data-line-number="2">mt3</a>
<a class="sourceLine" id="cb189-3" data-line-number="3"><span class="co">#&gt;      mpg cyl  disp  hp drat    wt  qsec vs am gear</span></a>
<a class="sourceLine" id="cb189-4" data-line-number="4"><span class="co">#&gt;  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4</span></a>
<a class="sourceLine" id="cb189-5" data-line-number="5"><span class="co">#&gt;  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4</span></a>
<a class="sourceLine" id="cb189-6" data-line-number="6"><span class="co">#&gt;  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4</span></a>
<a class="sourceLine" id="cb189-7" data-line-number="7"><span class="co">#&gt;  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3</span></a>
<a class="sourceLine" id="cb189-8" data-line-number="8"><span class="co">#&gt;  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3</span></a>
<a class="sourceLine" id="cb189-9" data-line-number="9"><span class="co">#&gt;  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3</span></a>
<a class="sourceLine" id="cb189-10" data-line-number="10"><span class="co">#&gt;  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3</span></a>
<a class="sourceLine" id="cb189-11" data-line-number="11"><span class="co">#&gt;  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4</span></a>
<a class="sourceLine" id="cb189-12" data-line-number="12"><span class="co">#&gt;  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4</span></a>
<a class="sourceLine" id="cb189-13" data-line-number="13"><span class="co">#&gt; 10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4</span></a>
<a class="sourceLine" id="cb189-14" data-line-number="14"><span class="co">#&gt; 11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4</span></a>
<a class="sourceLine" id="cb189-15" data-line-number="15"><span class="co">#&gt; 12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3</span></a>
<a class="sourceLine" id="cb189-16" data-line-number="16"><span class="co">#&gt; 13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3</span></a>
<a class="sourceLine" id="cb189-17" data-line-number="17"><span class="co">#&gt; 14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3</span></a>
<a class="sourceLine" id="cb189-18" data-line-number="18"><span class="co">#&gt; 15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3</span></a>
<a class="sourceLine" id="cb189-19" data-line-number="19"><span class="co">#&gt; 16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3</span></a>
<a class="sourceLine" id="cb189-20" data-line-number="20"><span class="co">#&gt; 17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3</span></a>
<a class="sourceLine" id="cb189-21" data-line-number="21"><span class="co">#&gt; 18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4</span></a>
<a class="sourceLine" id="cb189-22" data-line-number="22"><span class="co">#&gt; 19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4</span></a>
<a class="sourceLine" id="cb189-23" data-line-number="23"><span class="co">#&gt; 20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4</span></a>
<a class="sourceLine" id="cb189-24" data-line-number="24"><span class="co">#&gt; 21: 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3</span></a>
<a class="sourceLine" id="cb189-25" data-line-number="25"><span class="co">#&gt; 22: 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3</span></a>
<a class="sourceLine" id="cb189-26" data-line-number="26"><span class="co">#&gt; 23: 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3</span></a>
<a class="sourceLine" id="cb189-27" data-line-number="27"><span class="co">#&gt; 24: 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3</span></a>
<a class="sourceLine" id="cb189-28" data-line-number="28"><span class="co">#&gt; 25: 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3</span></a>
<a class="sourceLine" id="cb189-29" data-line-number="29"><span class="co">#&gt; 26: 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4</span></a>
<a class="sourceLine" id="cb189-30" data-line-number="30"><span class="co">#&gt; 27: 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5</span></a>
<a class="sourceLine" id="cb189-31" data-line-number="31"><span class="co">#&gt; 28: 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5</span></a>
<a class="sourceLine" id="cb189-32" data-line-number="32"><span class="co">#&gt; 29: 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5</span></a>
<a class="sourceLine" id="cb189-33" data-line-number="33"><span class="co">#&gt; 30: 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5</span></a>
<a class="sourceLine" id="cb189-34" data-line-number="34"><span class="co">#&gt; 31: 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5</span></a>
<a class="sourceLine" id="cb189-35" data-line-number="35"><span class="co">#&gt; 32: 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4</span></a>
<a class="sourceLine" id="cb189-36" data-line-number="36"><span class="co">#&gt;      mpg cyl  disp  hp drat    wt  qsec vs am gear</span></a>
<a class="sourceLine" id="cb189-37" data-line-number="37"><span class="co">#&gt;     carb</span></a>
<a class="sourceLine" id="cb189-38" data-line-number="38"><span class="co">#&gt;  1:    4</span></a>
<a class="sourceLine" id="cb189-39" data-line-number="39"><span class="co">#&gt;  2:    4</span></a>
<a class="sourceLine" id="cb189-40" data-line-number="40"><span class="co">#&gt;  3:    1</span></a>
<a class="sourceLine" id="cb189-41" data-line-number="41"><span class="co">#&gt;  4:    1</span></a>
<a class="sourceLine" id="cb189-42" data-line-number="42"><span class="co">#&gt;  5:    2</span></a>
<a class="sourceLine" id="cb189-43" data-line-number="43"><span class="co">#&gt;  6:    1</span></a>
<a class="sourceLine" id="cb189-44" data-line-number="44"><span class="co">#&gt;  7:    4</span></a>
<a class="sourceLine" id="cb189-45" data-line-number="45"><span class="co">#&gt;  8:    2</span></a>
<a class="sourceLine" id="cb189-46" data-line-number="46"><span class="co">#&gt;  9:    2</span></a>
<a class="sourceLine" id="cb189-47" data-line-number="47"><span class="co">#&gt; 10:    4</span></a>
<a class="sourceLine" id="cb189-48" data-line-number="48"><span class="co">#&gt; 11:    4</span></a>
<a class="sourceLine" id="cb189-49" data-line-number="49"><span class="co">#&gt; 12:    3</span></a>
<a class="sourceLine" id="cb189-50" data-line-number="50"><span class="co">#&gt; 13:    3</span></a>
<a class="sourceLine" id="cb189-51" data-line-number="51"><span class="co">#&gt; 14:    3</span></a>
<a class="sourceLine" id="cb189-52" data-line-number="52"><span class="co">#&gt; 15:    4</span></a>
<a class="sourceLine" id="cb189-53" data-line-number="53"><span class="co">#&gt; 16:    4</span></a>
<a class="sourceLine" id="cb189-54" data-line-number="54"><span class="co">#&gt; 17:    4</span></a>
<a class="sourceLine" id="cb189-55" data-line-number="55"><span class="co">#&gt; 18:    1</span></a>
<a class="sourceLine" id="cb189-56" data-line-number="56"><span class="co">#&gt; 19:    2</span></a>
<a class="sourceLine" id="cb189-57" data-line-number="57"><span class="co">#&gt; 20:    1</span></a>
<a class="sourceLine" id="cb189-58" data-line-number="58"><span class="co">#&gt; 21:    1</span></a>
<a class="sourceLine" id="cb189-59" data-line-number="59"><span class="co">#&gt; 22:    2</span></a>
<a class="sourceLine" id="cb189-60" data-line-number="60"><span class="co">#&gt; 23:    2</span></a>
<a class="sourceLine" id="cb189-61" data-line-number="61"><span class="co">#&gt; 24:    4</span></a>
<a class="sourceLine" id="cb189-62" data-line-number="62"><span class="co">#&gt; 25:    2</span></a>
<a class="sourceLine" id="cb189-63" data-line-number="63"><span class="co">#&gt; 26:    1</span></a>
<a class="sourceLine" id="cb189-64" data-line-number="64"><span class="co">#&gt; 27:    2</span></a>
<a class="sourceLine" id="cb189-65" data-line-number="65"><span class="co">#&gt; 28:    2</span></a>
<a class="sourceLine" id="cb189-66" data-line-number="66"><span class="co">#&gt; 29:    4</span></a>
<a class="sourceLine" id="cb189-67" data-line-number="67"><span class="co">#&gt; 30:    6</span></a>
<a class="sourceLine" id="cb189-68" data-line-number="68"><span class="co">#&gt; 31:    8</span></a>
<a class="sourceLine" id="cb189-69" data-line-number="69"><span class="co">#&gt; 32:    2</span></a>
<a class="sourceLine" id="cb189-70" data-line-number="70"><span class="co">#&gt;     carb</span></a></code></pre></div>
<p>为什么 <code>fread()</code> 没有设置分隔符却可以导入 CSV 和 TSV 文件？其中的巧妙在于该函数的内部会自动检测数据文件内的分隔符号。</p>
<p>通过查看该函数的参数，我们可以看到 <code>sep = &quot;auto&quot;</code>，这个参数我们可以人为指定，以适应不同的需求场景。</p>
<div class="sourceCode" id="cb190"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb190-1" data-line-number="1"><span class="kw">args</span>(fread)</a>
<a class="sourceLine" id="cb190-2" data-line-number="2"><span class="co">#&gt; function (input = &quot;&quot;, file = NULL, text = NULL, cmd = NULL, sep = &quot;auto&quot;, </span></a>
<a class="sourceLine" id="cb190-3" data-line-number="3"><span class="co">#&gt;     sep2 = &quot;auto&quot;, dec = &quot;.&quot;, quote = &quot;\&quot;&quot;, nrows = Inf, header = &quot;auto&quot;, </span></a>
<a class="sourceLine" id="cb190-4" data-line-number="4"><span class="co">#&gt;     na.strings = getOption(&quot;datatable.na.strings&quot;, &quot;NA&quot;), stringsAsFactors = FALSE, </span></a>
<a class="sourceLine" id="cb190-5" data-line-number="5"><span class="co">#&gt;     verbose = getOption(&quot;datatable.verbose&quot;, FALSE), skip = &quot;__auto__&quot;, </span></a>
<a class="sourceLine" id="cb190-6" data-line-number="6"><span class="co">#&gt;     select = NULL, drop = NULL, colClasses = NULL, integer64 = getOption(&quot;datatable.integer64&quot;, </span></a>
<a class="sourceLine" id="cb190-7" data-line-number="7"><span class="co">#&gt;         &quot;integer64&quot;), col.names, check.names = FALSE, encoding = &quot;unknown&quot;, </span></a>
<a class="sourceLine" id="cb190-8" data-line-number="8"><span class="co">#&gt;     strip.white = TRUE, fill = FALSE, blank.lines.skip = FALSE, </span></a>
<a class="sourceLine" id="cb190-9" data-line-number="9"><span class="co">#&gt;     key = NULL, index = NULL, showProgress = getOption(&quot;datatable.showProgress&quot;, </span></a>
<a class="sourceLine" id="cb190-10" data-line-number="10"><span class="co">#&gt;         interactive()), data.table = getOption(&quot;datatable.fread.datatable&quot;, </span></a>
<a class="sourceLine" id="cb190-11" data-line-number="11"><span class="co">#&gt;         TRUE), nThread = getDTthreads(verbose), logical01 = getOption(&quot;datatable.logical01&quot;, </span></a>
<a class="sourceLine" id="cb190-12" data-line-number="12"><span class="co">#&gt;         FALSE), keepLeadingZeros = getOption(&quot;datatable.keepLeadingZeros&quot;, </span></a>
<a class="sourceLine" id="cb190-13" data-line-number="13"><span class="co">#&gt;         FALSE), yaml = FALSE, autostart = NA, tmpdir = tempdir()) </span></a>
<a class="sourceLine" id="cb190-14" data-line-number="14"><span class="co">#&gt; NULL</span></a></code></pre></div>
<p>上面展示出 <code>fread()</code> 存在众多的参数设置。R 的易用与友好在于此，众多 R 包提供的函数大多已经为用户恰当地设置好了默认选项，用户无需经过繁琐的操作即可快速获取所需的结果。</p>
<p>在学习了如何导入 TSV 文件后，我们应该能够归纳出 CSV、TSV 本质上是一类数据格式文件。例如，我们也可以使用分号 <code>;</code>（西方不少国家的逗号分隔文件就是以分号分隔的文件）。我们统一以 CSV 作为代表，将其他类似格式文件称为 CSV 变体。</p>
</div>
</div>
<div id="excel" class="section level2">
<h2><span class="header-section-number">3.2</span> Excel</h2>
<p><strong>Excel</strong> 是知名的微软 Office 套件之一，提供了日常的表格数据处理方案。尽管大多数数据分析人员不会产生和不想处理 Excel 文件，但由于它随着微软系统的流行而被广泛使用，因此读入 Excel 中的数据成为数据处理无法避免的日常工作任务之一。</p>
<p>R 中有诸多拓展包可以导入 Excel 中的数据，其中最为推荐的就是本部分介绍的 <a href="https://github.com/tidyverse/readxl"><strong>readxl</strong></a> 包。</p>
<p>使用该包，导入 Excel 中的数据可以像读入 CSV 文件一样简单。</p>
<div class="sourceCode" id="cb191"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb191-1" data-line-number="1"><span class="kw">library</span>(readxl)</a>
<a class="sourceLine" id="cb191-2" data-line-number="2">mt_excel &lt;-<span class="st"> </span><span class="kw">read_excel</span>(<span class="st">&quot;data/data-import/mtcars.xlsx&quot;</span>)</a>
<a class="sourceLine" id="cb191-3" data-line-number="3"><span class="kw">head</span>(mt_excel)</a>
<a class="sourceLine" id="cb191-4" data-line-number="4"><span class="co">#&gt; # A tibble: 6 x 11</span></a>
<a class="sourceLine" id="cb191-5" data-line-number="5"><span class="co">#&gt;     mpg   cyl  disp    hp  drat    wt  qsec    vs    am</span></a>
<a class="sourceLine" id="cb191-6" data-line-number="6"><span class="co">#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></a>
<a class="sourceLine" id="cb191-7" data-line-number="7"><span class="co">#&gt; 1  21       6   160   110  3.9   2.62  16.5     0     1</span></a>
<a class="sourceLine" id="cb191-8" data-line-number="8"><span class="co">#&gt; 2  21       6   160   110  3.9   2.88  17.0     0     1</span></a>
<a class="sourceLine" id="cb191-9" data-line-number="9"><span class="co">#&gt; 3  22.8     4   108    93  3.85  2.32  18.6     1     1</span></a>
<a class="sourceLine" id="cb191-10" data-line-number="10"><span class="co">#&gt; 4  21.4     6   258   110  3.08  3.22  19.4     1     0</span></a>
<a class="sourceLine" id="cb191-11" data-line-number="11"><span class="co">#&gt; 5  18.7     8   360   175  3.15  3.44  17.0     0     0</span></a>
<a class="sourceLine" id="cb191-12" data-line-number="12"><span class="co">#&gt; 6  18.1     6   225   105  2.76  3.46  20.2     1     0</span></a>
<a class="sourceLine" id="cb191-13" data-line-number="13"><span class="co">#&gt; # … with 2 more variables: gear &lt;dbl&gt;, carb &lt;dbl&gt;</span></a></code></pre></div>
<p><code>read_excel()</code> 同时支持 <code>.xls</code> 和 <code>xlsx</code> 两者文件格式。</p>
<p>Excel 文件支持多个表格（Sheet），这种情况下默认第 1 个 Sheet 的内容会被读入。通过简单的设置，读者也可以轻松导入任意 Sheet 的内容。</p>
<p>下面通过 <strong>readxl</strong> 包的官方示例展示这一过程。</p>
<p>我们先查看下该包提供的示例数据：</p>
<div class="sourceCode" id="cb192"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb192-1" data-line-number="1"><span class="kw">readxl_example</span>()</a>
<a class="sourceLine" id="cb192-2" data-line-number="2"><span class="co">#&gt;  [1] &quot;clippy.xls&quot;    &quot;clippy.xlsx&quot;   &quot;datasets.xls&quot; </span></a>
<a class="sourceLine" id="cb192-3" data-line-number="3"><span class="co">#&gt;  [4] &quot;datasets.xlsx&quot; &quot;deaths.xls&quot;    &quot;deaths.xlsx&quot;  </span></a>
<a class="sourceLine" id="cb192-4" data-line-number="4"><span class="co">#&gt;  [7] &quot;geometry.xls&quot;  &quot;geometry.xlsx&quot; &quot;type-me.xls&quot;  </span></a>
<a class="sourceLine" id="cb192-5" data-line-number="5"><span class="co">#&gt; [10] &quot;type-me.xlsx&quot;</span></a></code></pre></div>
<p>选一个文件并查看其所在的路径：</p>
<div class="sourceCode" id="cb193"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb193-1" data-line-number="1"><span class="kw">readxl_example</span>(<span class="st">&quot;datasets.xlsx&quot;</span>)</a>
<a class="sourceLine" id="cb193-2" data-line-number="2"><span class="co">#&gt; [1] &quot;/Users/wsx/R_Library/readxl/extdata/datasets.xlsx&quot;</span></a>
<a class="sourceLine" id="cb193-3" data-line-number="3"><span class="co"># 将路径保存备用</span></a>
<a class="sourceLine" id="cb193-4" data-line-number="4">excel_path &lt;-<span class="st"> </span><span class="kw">readxl_example</span>(<span class="st">&quot;datasets.xlsx&quot;</span>)</a></code></pre></div>
<p>如果将路径传入 <code>excel_sheets()</code> 函数，读者可以获取文件中存在的 Sheets。</p>
<div class="sourceCode" id="cb194"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb194-1" data-line-number="1"><span class="kw">excel_sheets</span>(excel_path)</a>
<a class="sourceLine" id="cb194-2" data-line-number="2"><span class="co">#&gt; [1] &quot;iris&quot;     &quot;mtcars&quot;   &quot;chickwts&quot; &quot;quakes&quot;</span></a></code></pre></div>
<p>然后依旧是使用 <code>read_excel()</code> 函数，设定想要读入的 Sheet 名字即可。</p>
<div class="sourceCode" id="cb195"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb195-1" data-line-number="1">iris &lt;-<span class="st"> </span><span class="kw">read_excel</span>(excel_path, <span class="dt">sheet =</span> <span class="st">&quot;iris&quot;</span>)</a>
<a class="sourceLine" id="cb195-2" data-line-number="2"><span class="kw">head</span>(iris)</a>
<a class="sourceLine" id="cb195-3" data-line-number="3"><span class="co">#&gt; # A tibble: 6 x 5</span></a>
<a class="sourceLine" id="cb195-4" data-line-number="4"><span class="co">#&gt;   Sepal.Length Sepal.Width Petal.Length Petal.Width</span></a>
<a class="sourceLine" id="cb195-5" data-line-number="5"><span class="co">#&gt;          &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt;</span></a>
<a class="sourceLine" id="cb195-6" data-line-number="6"><span class="co">#&gt; 1          5.1         3.5          1.4         0.2</span></a>
<a class="sourceLine" id="cb195-7" data-line-number="7"><span class="co">#&gt; 2          4.9         3            1.4         0.2</span></a>
<a class="sourceLine" id="cb195-8" data-line-number="8"><span class="co">#&gt; 3          4.7         3.2          1.3         0.2</span></a>
<a class="sourceLine" id="cb195-9" data-line-number="9"><span class="co">#&gt; 4          4.6         3.1          1.5         0.2</span></a>
<a class="sourceLine" id="cb195-10" data-line-number="10"><span class="co">#&gt; 5          5           3.6          1.4         0.2</span></a>
<a class="sourceLine" id="cb195-11" data-line-number="11"><span class="co">#&gt; 6          5.4         3.9          1.7         0.4</span></a>
<a class="sourceLine" id="cb195-12" data-line-number="12"><span class="co">#&gt; # … with 1 more variable: Species &lt;chr&gt;</span></a></code></pre></div>
<p>关于函数的更多用法读者可以通过 <code>?read_excel()</code> 查看。</p>
<p>有时候人们会在同一个 Excel Sheet 中放置多个表格信息，这种情况无法通过简单地使用 <strong>readxl</strong> 包读入数据。只要数据是有规律的，读者可以尝试下通过 <a href="https://github.com/r-rudra/tidycells"><strong>tidycells</strong></a> 包导入数据。</p>
<div class="figure" style="text-align: center"><span id="fig:unnamed-chunk-185"></span>
<img src="https://github.com/r-rudra/tidycells/raw/master/vignettes/ext/marks.png" alt="tidycells 包示例数据格式"  />
<p class="caption">
图 3.1: tidycells 包示例数据格式
</p>
</div>
<p>具体的使用请读者参照 <a href="https://github.com/r-rudra/tidycells/blob/master/README.md">README</a> 进行学习。</p>
</div>
<div id="json" class="section level2">
<h2><span class="header-section-number">3.3</span> JSON</h2>
<p>JSON 是目前使用最广泛的网络数据交换格式，它非常轻量，易于阅读和编写。与 R 不同，它有自己的数据结构存储数值、字符串、布尔值、数组等信息。关于它的介绍请读者阅读<a href="https://www.json.org/json-zh.html">官网文档</a>，这里我们仅介绍如何将其导入 R 中以便于分析。</p>
<p>R 中最流行的 JSON 数据解析包是 <strong>jsonlite</strong>，它非常轻量，但功能强大：支持 JSON 格式数据的解析以及将 R 中的数据结构编码为 JSON 格式。</p>
<p>下面的例子展示了 R 中常见数据结构解析为 JSON 格式后的结果：</p>
<div class="sourceCode" id="cb196"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb196-1" data-line-number="1">jsonlite<span class="op">::</span><span class="kw">toJSON</span>(letters)</a>
<a class="sourceLine" id="cb196-2" data-line-number="2"><span class="co">#&gt; [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;,&quot;g&quot;,&quot;h&quot;,&quot;i&quot;,&quot;j&quot;,&quot;k&quot;,&quot;l&quot;,&quot;m&quot;,&quot;n&quot;,&quot;o&quot;,&quot;p&quot;,&quot;q&quot;,&quot;r&quot;,&quot;s&quot;,&quot;t&quot;,&quot;u&quot;,&quot;v&quot;,&quot;w&quot;,&quot;x&quot;,&quot;y&quot;,&quot;z&quot;]</span></a>
<a class="sourceLine" id="cb196-3" data-line-number="3">jsonlite<span class="op">::</span><span class="kw">toJSON</span>(<span class="kw">c</span>(<span class="dt">a =</span> 1L, <span class="dt">b =</span> <span class="fl">2.0</span>))</a>
<a class="sourceLine" id="cb196-4" data-line-number="4"><span class="co">#&gt; [1,2]</span></a>
<a class="sourceLine" id="cb196-5" data-line-number="5">jsonlite<span class="op">::</span><span class="kw">toJSON</span>(<span class="kw">data.frame</span>(<span class="dt">a =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="dt">b =</span> <span class="dv">2</span><span class="op">:</span><span class="dv">4</span>))</a>
<a class="sourceLine" id="cb196-6" data-line-number="6"><span class="co">#&gt; [{&quot;a&quot;:1,&quot;b&quot;:2},{&quot;a&quot;:2,&quot;b&quot;:3},{&quot;a&quot;:3,&quot;b&quot;:4}]</span></a>
<a class="sourceLine" id="cb196-7" data-line-number="7">jsonlite<span class="op">::</span><span class="kw">toJSON</span>(<span class="kw">list</span>(<span class="dt">a =</span> 1L, <span class="dt">b =</span> <span class="dv">2</span><span class="op">:</span><span class="dv">5</span>, <span class="dt">c =</span> <span class="kw">c</span>(<span class="ot">TRUE</span>, <span class="ot">FALSE</span>), <span class="dt">d =</span> <span class="ot">NULL</span>))</a>
<a class="sourceLine" id="cb196-8" data-line-number="8"><span class="co">#&gt; {&quot;a&quot;:[1],&quot;b&quot;:[2,3,4,5],&quot;c&quot;:[true,false],&quot;d&quot;:{}}</span></a></code></pre></div>
<p>使用 <code>write_json()</code> 函数即可将 JSON 数据保存到本地：</p>
<div class="sourceCode" id="cb197"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb197-1" data-line-number="1">jsonlite<span class="op">::</span><span class="kw">write_json</span>(<span class="kw">list</span>(<span class="dt">a =</span> 1L, <span class="dt">b =</span> <span class="dv">2</span><span class="op">:</span><span class="dv">5</span>, <span class="dt">c =</span> <span class="kw">c</span>(<span class="ot">TRUE</span>, <span class="ot">FALSE</span>), <span class="dt">d =</span> <span class="ot">NULL</span>),</a>
<a class="sourceLine" id="cb197-2" data-line-number="2">                     <span class="dt">path =</span> <span class="st">&quot;data/data-import/example.json&quot;</span>)</a></code></pre></div>
<p>有了示例的 JSON 文件，接下来我们看如何将其导入 R 中：</p>
<div class="sourceCode" id="cb198"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb198-1" data-line-number="1">jsonlite<span class="op">::</span><span class="kw">read_json</span>(<span class="st">&quot;data/data-import/example.json&quot;</span>)</a>
<a class="sourceLine" id="cb198-2" data-line-number="2"><span class="co">#&gt; $a</span></a>
<a class="sourceLine" id="cb198-3" data-line-number="3"><span class="co">#&gt; $a[[1]]</span></a>
<a class="sourceLine" id="cb198-4" data-line-number="4"><span class="co">#&gt; [1] 1</span></a>
<a class="sourceLine" id="cb198-5" data-line-number="5"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-6" data-line-number="6"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-7" data-line-number="7"><span class="co">#&gt; $b</span></a>
<a class="sourceLine" id="cb198-8" data-line-number="8"><span class="co">#&gt; $b[[1]]</span></a>
<a class="sourceLine" id="cb198-9" data-line-number="9"><span class="co">#&gt; [1] 2</span></a>
<a class="sourceLine" id="cb198-10" data-line-number="10"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-11" data-line-number="11"><span class="co">#&gt; $b[[2]]</span></a>
<a class="sourceLine" id="cb198-12" data-line-number="12"><span class="co">#&gt; [1] 3</span></a>
<a class="sourceLine" id="cb198-13" data-line-number="13"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-14" data-line-number="14"><span class="co">#&gt; $b[[3]]</span></a>
<a class="sourceLine" id="cb198-15" data-line-number="15"><span class="co">#&gt; [1] 4</span></a>
<a class="sourceLine" id="cb198-16" data-line-number="16"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-17" data-line-number="17"><span class="co">#&gt; $b[[4]]</span></a>
<a class="sourceLine" id="cb198-18" data-line-number="18"><span class="co">#&gt; [1] 5</span></a>
<a class="sourceLine" id="cb198-19" data-line-number="19"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-20" data-line-number="20"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-21" data-line-number="21"><span class="co">#&gt; $c</span></a>
<a class="sourceLine" id="cb198-22" data-line-number="22"><span class="co">#&gt; $c[[1]]</span></a>
<a class="sourceLine" id="cb198-23" data-line-number="23"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb198-24" data-line-number="24"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-25" data-line-number="25"><span class="co">#&gt; $c[[2]]</span></a>
<a class="sourceLine" id="cb198-26" data-line-number="26"><span class="co">#&gt; [1] FALSE</span></a>
<a class="sourceLine" id="cb198-27" data-line-number="27"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-28" data-line-number="28"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb198-29" data-line-number="29"><span class="co">#&gt; $d</span></a>
<a class="sourceLine" id="cb198-30" data-line-number="30"><span class="co">#&gt; named list()</span></a></code></pre></div>
<p>不难发现，JSON 文件中的信息被保存到了列表之中，但结构过于复杂，我们可以用 <code>simplifyVector</code> 选项进行简化：</p>
<div class="sourceCode" id="cb199"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb199-1" data-line-number="1">jsonlite<span class="op">::</span><span class="kw">read_json</span>(<span class="st">&quot;data/data-import/example.json&quot;</span>, <span class="dt">simplifyVector =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb199-2" data-line-number="2"><span class="co">#&gt; $a</span></a>
<a class="sourceLine" id="cb199-3" data-line-number="3"><span class="co">#&gt; [1] 1</span></a>
<a class="sourceLine" id="cb199-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb199-5" data-line-number="5"><span class="co">#&gt; $b</span></a>
<a class="sourceLine" id="cb199-6" data-line-number="6"><span class="co">#&gt; [1] 2 3 4 5</span></a>
<a class="sourceLine" id="cb199-7" data-line-number="7"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb199-8" data-line-number="8"><span class="co">#&gt; $c</span></a>
<a class="sourceLine" id="cb199-9" data-line-number="9"><span class="co">#&gt; [1]  TRUE FALSE</span></a>
<a class="sourceLine" id="cb199-10" data-line-number="10"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb199-11" data-line-number="11"><span class="co">#&gt; $d</span></a>
<a class="sourceLine" id="cb199-12" data-line-number="12"><span class="co">#&gt; named list()</span></a></code></pre></div>
<p>如果 JSON 文件结构非常复杂，那么导入 R 以后形成的列表也会极其复杂，读者需要良好的列表处理功底才能进行处理。</p>
</div>
<div id="r-数据文件" class="section level2">
<h2><span class="header-section-number">3.4</span> R 数据文件</h2>
<p>使用 R 自身提供的数据存储格式是一种有效且常见的方式，非常适合数据分析项目。相比于通用格式如 CSV，它可以避免外部程序的删改，且有效利用磁盘存储空间。
R 内置的格式有 RData 和 RDS 两种。</p>
<div id="rdata" class="section level3">
<h3><span class="header-section-number">3.4.1</span> RData</h3>
<p>RData 可能是读者最常见的一种 R 存储格式，它用于保存用户空间一个或多个对象。存储时使用 <code>save()</code> 函数，导入时使用 <code>load()</code> 函数。</p>
<div class="sourceCode" id="cb200"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb200-1" data-line-number="1"><span class="co">## 清除空间所有对象</span></a>
<a class="sourceLine" id="cb200-2" data-line-number="2"><span class="kw">rm</span>(<span class="dt">list =</span> <span class="kw">ls</span>())</a>
<a class="sourceLine" id="cb200-3" data-line-number="3"><span class="co">## 创建两个对象</span></a>
<a class="sourceLine" id="cb200-4" data-line-number="4">d1 &lt;-<span class="st"> </span>mtcars</a>
<a class="sourceLine" id="cb200-5" data-line-number="5">d2 &lt;-<span class="st"> </span>mtcars</a>
<a class="sourceLine" id="cb200-6" data-line-number="6"><span class="kw">ls</span>()</a>
<a class="sourceLine" id="cb200-7" data-line-number="7"><span class="co">#&gt; [1] &quot;d1&quot; &quot;d2&quot;</span></a></code></pre></div>
<p>上面我们在空间创建了 2 个示例对象，我们将其保存如下：</p>
<div class="sourceCode" id="cb201"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb201-1" data-line-number="1"><span class="kw">save</span>(d1, d2, <span class="dt">file =</span> <span class="st">&quot;data/data-import/mtcars.RData&quot;</span>)</a></code></pre></div>
<p>这里注意一定要写明 <code>file =</code>，这是一个新手常犯的错误，如果不指明参数名称，路径将被看作一个对象，这会导致报错：</p>
<div class="sourceCode" id="cb202"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb202-1" data-line-number="1"><span class="kw">save</span>(d1, d2, <span class="st">&quot;data/data-import/mtcars.RData&quot;</span>)</a>
<a class="sourceLine" id="cb202-2" data-line-number="2"><span class="co">#&gt; Error in save(d1, d2, &quot;data/data-import/mtcars.RData&quot;): 目标对象&#39;data/data-import/mtcars.RData&#39;不存在</span></a></code></pre></div>
<p>导入时这一操作则是非必需的：</p>
<div class="sourceCode" id="cb203"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb203-1" data-line-number="1"><span class="kw">rm</span>(<span class="dt">list =</span> <span class="kw">ls</span>())</a>
<a class="sourceLine" id="cb203-2" data-line-number="2"></a>
<a class="sourceLine" id="cb203-3" data-line-number="3"><span class="kw">load</span>(<span class="st">&quot;data/data-import/mtcars.RData&quot;</span>)</a>
<a class="sourceLine" id="cb203-4" data-line-number="4"></a>
<a class="sourceLine" id="cb203-5" data-line-number="5"><span class="kw">ls</span>()</a>
<a class="sourceLine" id="cb203-6" data-line-number="6"><span class="co">#&gt; [1] &quot;d1&quot; &quot;d2&quot;</span></a></code></pre></div>
<p>这里我们之前保存的两个对象都会被直接导入环境空间，对象名称也不会改变。如果读者想要迅速保存当前环境所有的对象，有效的方式是使用 <code>save.image()</code> 函数。</p>
</div>
<div id="rds" class="section level3">
<h3><span class="header-section-number">3.4.2</span> RDS</h3>
<p>有时候我们想要保存单个的对象，且在导入时可以直接重命名该对象，这时我们可以用 RDS 格式。存储时使用 <code>saveRDS()</code> 函数，导入时使用 <code>readRDS()</code> 函数。</p>
<div class="sourceCode" id="cb204"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb204-1" data-line-number="1"><span class="kw">saveRDS</span>(mtcars, <span class="dt">file =</span> <span class="st">&quot;data/data-import/mtcars.rds&quot;</span>)</a>
<a class="sourceLine" id="cb204-2" data-line-number="2">mtcars_rename &lt;-<span class="st"> </span><span class="kw">readRDS</span>(<span class="st">&quot;data/data-import/mtcars.rds&quot;</span>)</a>
<a class="sourceLine" id="cb204-3" data-line-number="3"></a>
<a class="sourceLine" id="cb204-4" data-line-number="4"><span class="kw">head</span>(mtcars_rename)</a>
<a class="sourceLine" id="cb204-5" data-line-number="5"><span class="co">#&gt;                    mpg cyl disp  hp drat    wt  qsec</span></a>
<a class="sourceLine" id="cb204-6" data-line-number="6"><span class="co">#&gt; Mazda RX4         21.0   6  160 110 3.90 2.620 16.46</span></a>
<a class="sourceLine" id="cb204-7" data-line-number="7"><span class="co">#&gt; Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02</span></a>
<a class="sourceLine" id="cb204-8" data-line-number="8"><span class="co">#&gt; Datsun 710        22.8   4  108  93 3.85 2.320 18.61</span></a>
<a class="sourceLine" id="cb204-9" data-line-number="9"><span class="co">#&gt; Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44</span></a>
<a class="sourceLine" id="cb204-10" data-line-number="10"><span class="co">#&gt; Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02</span></a>
<a class="sourceLine" id="cb204-11" data-line-number="11"><span class="co">#&gt; Valiant           18.1   6  225 105 2.76 3.460 20.22</span></a>
<a class="sourceLine" id="cb204-12" data-line-number="12"><span class="co">#&gt;                   vs am gear carb</span></a>
<a class="sourceLine" id="cb204-13" data-line-number="13"><span class="co">#&gt; Mazda RX4          0  1    4    4</span></a>
<a class="sourceLine" id="cb204-14" data-line-number="14"><span class="co">#&gt; Mazda RX4 Wag      0  1    4    4</span></a>
<a class="sourceLine" id="cb204-15" data-line-number="15"><span class="co">#&gt; Datsun 710         1  1    4    1</span></a>
<a class="sourceLine" id="cb204-16" data-line-number="16"><span class="co">#&gt; Hornet 4 Drive     1  0    3    1</span></a>
<a class="sourceLine" id="cb204-17" data-line-number="17"><span class="co">#&gt; Hornet Sportabout  0  0    3    2</span></a>
<a class="sourceLine" id="cb204-18" data-line-number="18"><span class="co">#&gt; Valiant            1  0    3    1</span></a></code></pre></div>
</div>
</div>
<div id="常见问题与方案-2" class="section level2">
<h2><span class="header-section-number">3.5</span> 常见问题与方案</h2>
<p>除了本节目前罗列的问题，读者在学习本章内容时遇到的其他问题都可以通过 <a href="https://github.com/ShixiangWang/geek-r-tutorial/issues">GitHub Issue</a> 提出和进行讨论。如果读者提出的是通性问题，将增补到该节。</p>
<div id="通过键盘和剪贴板载入数据" class="section level3">
<h3><span class="header-section-number">3.5.1</span> 通过键盘和剪贴板载入数据</h3>
<p>如果读者想要从键盘读取数据，我们需要使用到标准输入 <code>stdin()</code> 函数进行辅助。</p>
<div class="sourceCode" id="cb205"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb205-1" data-line-number="1">data &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="kw">stdin</span>())</a>
<a class="sourceLine" id="cb205-2" data-line-number="2"><span class="co">## 运行后输入文本</span></a></code></pre></div>
<p>这种操作并不好用，稍微好一点的是先将数据拷贝到剪切板，然后使用下面的命令读入：</p>
<div class="sourceCode" id="cb206"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb206-1" data-line-number="1">data &lt;-<span class="st"> </span><span class="kw">read.table</span>(<span class="st">&#39;clipboard&#39;</span>, <span class="dt">header=</span><span class="ot">TRUE</span>)</a></code></pre></div>
</div>
<div id="逐行读取数据" class="section level3">
<h3><span class="header-section-number">3.5.2</span> 逐行读取数据</h3>
<p>如果读者想要简单地从用户输入交互地读取一些信息，<code>readline()</code> 是一个好办法，例如：</p>
<div class="sourceCode" id="cb207"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb207-1" data-line-number="1"><span class="op">&gt;</span><span class="st"> </span><span class="kw">readline</span>(<span class="st">&quot;输入年龄：&quot;</span>)</a>
<a class="sourceLine" id="cb207-2" data-line-number="2">输入年龄：<span class="dv">22</span></a>
<a class="sourceLine" id="cb207-3" data-line-number="3">[<span class="dv">1</span>] <span class="st">&quot;22&quot;</span></a></code></pre></div>
<p>如果是想要将文件的内容按行的方式读取进来，需要使用 <code>readLines()</code> 函数。</p>
<p>下面是一个来自 <code>readLines()</code> 的官方示例：</p>
<div class="sourceCode" id="cb208"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb208-1" data-line-number="1">fil &lt;-<span class="st"> </span><span class="kw">tempfile</span>(<span class="dt">fileext =</span> <span class="st">&quot;.data&quot;</span>)</a>
<a class="sourceLine" id="cb208-2" data-line-number="2"><span class="kw">cat</span>(<span class="st">&quot;TITLE extra line&quot;</span>, <span class="st">&quot;2 3 5 7&quot;</span>, <span class="st">&quot;&quot;</span>, <span class="st">&quot;11 13 17&quot;</span>, <span class="dt">file =</span> fil,</a>
<a class="sourceLine" id="cb208-3" data-line-number="3">    <span class="dt">sep =</span> <span class="st">&quot;</span><span class="ch">\n</span><span class="st">&quot;</span>)</a>
<a class="sourceLine" id="cb208-4" data-line-number="4"><span class="kw">readLines</span>(fil, <span class="dt">n =</span> <span class="dv">-1</span>)</a>
<a class="sourceLine" id="cb208-5" data-line-number="5"><span class="co">#&gt; [1] &quot;TITLE extra line&quot; &quot;2 3 5 7&quot;         </span></a>
<a class="sourceLine" id="cb208-6" data-line-number="6"><span class="co">#&gt; [3] &quot;&quot;                 &quot;11 13 17&quot;</span></a>
<a class="sourceLine" id="cb208-7" data-line-number="7"><span class="kw">unlink</span>(fil) <span class="co"># 清理文件</span></a></code></pre></div>
</div>
<div id="读取等宽格式数据" class="section level3">
<h3><span class="header-section-number">3.5.3</span> 读取等宽格式数据</h3>
<p>有时候我们会碰到数据的列宽度是固定的，我们称它为定宽文本文件，像下面这样：</p>
<pre><code>  First     Last  Sex Number
 Currer     Bell    F      2
    Dr.    Seuss    M     49
    &quot;&quot;   Student   NA     21</code></pre>
<p>导入的方法这里推荐使用 2 种：</p>
<ol style="list-style-type: decimal">
<li>R 内置的 <code>read.fwf()</code> 函数。</li>
<li><strong>readr</strong> 包提供的 <code>read_fwf()</code>。</li>
</ol>
<p>详细的使用方法请读者参考文档学习。</p>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="base.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="clean.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook/js/app.min.js"></script>
<script src="libs/gitbook/js/lunr.js"></script>
<script src="libs/gitbook/js/clipboard.min.js"></script>
<script src="libs/gitbook/js/plugin-search.js"></script>
<script src="libs/gitbook/js/plugin-sharing.js"></script>
<script src="libs/gitbook/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook/js/plugin-bookdown.js"></script>
<script src="libs/gitbook/js/jquery.highlight.js"></script>
<script src="libs/gitbook/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/ShixiangWang/geek-r-tutorial/edit/master/03-data-import.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
